//
//  LotteryPeopleHeadView.m
//  WanCaiC8
//
//  Created by tangang on 2018/7/20.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "LotteryPeopleHeadView.h"
#import "NewsMasterCell.h"
#import "PlanPeopleVC.h"
#import "LotteryTypeModel.h"
#import "InformationVM.h"

#import "AdvertisingView.h"
@interface LotteryPeopleHeadView()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic ,strong) UICollectionView *peopleCollectionView;
@property (nonatomic ,strong) NSMutableArray    *peopleArr;

@property(nonatomic,strong)AdvertisingView *oneAdvertisingView;

@property(nonatomic,strong)NSMutableArray *btnArr;
@property(nonatomic,strong)UIView *btnView;
@property(nonatomic,strong)UIView *adBgView;
@end
@implementation LotteryPeopleHeadView{
    QXLable *labTitle;
    
    UIView *titleBgView;
    
}


-(void)setBtnAdModelArr:(NSMutableArray *)btnAdModelArr{
    
    _btnAdModelArr  = btnAdModelArr;
    for (int i = 0; i< btnAdModelArr.count; i++) {
        
        AdvertisingModel *model = btnAdModelArr[i];
        UIButton *btn = [self.btnView viewWithTag:1001+i];
        
       
        if ([model.Title containsString:@"【"]) {
            
            [btn setAttributedTitle: [NSAttributedString tg_imageTextWithTitle:model.Title] forState:UIControlStateNormal];
    
        } else {
            
             [btn setTitle:model.Title forState:UIControlStateNormal];
    
        }
       
    }
    
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {

        
        [self setupUI];
        
        [self getMasterList];
    }
    return self;
}

-(void)setupUI{
    self.adBgView = [[UIView alloc] init];
    self.adBgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.adBgView];
    [self.adBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(0);
    }];
    
    [self.oneAdvertisingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.adBgView).offset(10);
        make.left.equalTo(self).offset(10);
        make.right.equalTo(self).offset(-10);
        make.height.mas_equalTo(@0);
        make.bottom.equalTo(self.adBgView).offset(-5);
    }];
    

    titleBgView = [[UIView alloc] init];
    [self addSubview:titleBgView];
    [titleBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.adBgView.mas_bottom);
        make.height.mas_equalTo(40);
    }];

    UIView *line=[[UIView alloc]init];
    line.backgroundColor=DEFAULTBGCOLOR;
    [titleBgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(titleBgView);
        make.height.mas_equalTo(@5);
    }];
//
    
    
    UIView *redLine=[[UIView alloc]init];
    redLine.backgroundColor=NAVCOLOR;
    [titleBgView addSubview:redLine];
    [redLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleBgView);
        make.left.equalTo(titleBgView).with.offset(10);
        make.size.mas_equalTo(CGSizeMake(2, 10));
    }];
//
    labTitle=[[QXLable alloc]initTitle];
    labTitle.textColor= NAVCOLOR;
    labTitle.text = @"高手推荐";

    [titleBgView addSubview:labTitle];
    [labTitle sizeToFit];
    [labTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(redLine.mas_right).with.offset(5);
        make.centerY.equalTo(redLine);
    }];
    
    
    
    self.btnView = [[UIView alloc] init];
    self.btnView.backgroundColor = [UIColor whiteColor];
    [titleBgView addSubview:self.btnView];
    [self.btnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(titleBgView).offset(-5);
//        make.centerY.equalTo(labTitle);
        make.left.equalTo(labTitle.mas_right);
        make.top.equalTo(line.mas_bottom);
        make.bottom.equalTo(titleBgView);
    }];
    self.btnArr = [NSMutableArray array];
    
    
    for (int i = 2; i>=0 ; i--) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i+1001;
        
        [btn.titleLabel setFont:[UIFont systemFontOfSize:13]];
        [btn setTitleColor:DEFAULTTEXTGDARKRAYCOLOR forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

        [self.btnView addSubview:btn];
        
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(labTitle);
        }];
        [self.btnArr addObject:btn];
        
    }
    
    [self.btnArr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:8 leadSpacing:0 tailSpacing:0];
    
    UIView *line1=[[UIView alloc]init];
    line1.backgroundColor=DEFAULTLINECOLOR;
    [self addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(titleBgView);
        make.height.mas_equalTo(@.7);
    }];
    
    
}


-(void)buttonClick:(UIButton *)btn{
    //按钮点击事件
    AdvertisingModel *model=self.btnAdModelArr[btn.tag-1001];
    
    if ([model.Title containsString:@"】"]) {
        
        UIPasteboard *copy = [UIPasteboard generalPasteboard];
        NSRange startRange = [model.Title rangeOfString:@"【"];
        NSRange endRange = [model.Title rangeOfString:@"】"];
        NSRange range = NSMakeRange(startRange.location + startRange.length, endRange.location - startRange.location - startRange.length);
        NSString *result = [model.Title substringWithRange:range];
        
        [copy setString:result];
        
        [SVProgressHUD showSuccessWithStatus:@"复制成功"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString: @"weixin://"]];
            
        });
    }else{
        if (model.TransferUrl.length<1) {
            return;
        }
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString: model.TransferUrl]];
    }
    
    
    
    
    
}

-(void)setAdModelArr:(NSMutableArray *)adModelArr AndRowCount:(NSInteger)rowCount{

    if (adModelArr.count>0) {
        self.oneAdvertisingView.dataArr = adModelArr;
        
        self.oneAdvertisingView.changeHeight(rowCount*40);
        
        [self.adBgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(rowCount*40+15);
        }];
    }
    
}

//-(void)setAdModelArr:(NSMutableArray *)adModelArr{
//
//    _adModelArr = adModelArr;
//
//    self.oneAdvertisingView.dataArr = adModelArr;
//
//    self.oneAdvertisingView.changeHeight(adModelArr.count*40);
//
//}

- (AdvertisingView *)oneAdvertisingView{
    if (!_oneAdvertisingView) {
        WS(weakSelf);
        _oneAdvertisingView =[[AdvertisingView alloc]init];
        [self.adBgView addSubview:_oneAdvertisingView];
        self.oneAdvertisingView.changeHeight = ^(float height) {
            [weakSelf.oneAdvertisingView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(@(height));
            }];
        };
    }
    return _oneAdvertisingView;
}

#pragma mark -- 获取推荐高手
- (void)getMasterList{
    WS(weakSelf);
    [[InformationVM manage] getRecommendMasterListWithLType:@"5" Success:^(id returnValue) {
        weakSelf.peopleArr=returnValue;
        [weakSelf.peopleCollectionView reloadData];
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
}

#pragma mark - collectionView代理函数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _peopleArr.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NewsMasterCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"NewsMasterCell" forIndexPath:indexPath];
    cell.model=self.peopleArr[indexPath.row];
    return cell;
}


-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ExpertsModel *eModel=self.peopleArr[indexPath.row];
    PlanPeopleVC *vc=[[PlanPeopleVC alloc]init];
    LotteryTypeModel *lotteryModel=[[LotteryTypeModel alloc]init];
    lotteryModel.lType=eModel.lType;
    vc.lotteryModel=lotteryModel;
    vc.model=eModel;
    vc.playName=eModel.PlayName;
    vc.title=@"六合彩近期竞彩";
    [[TGTools viewController:self].navigationController pushViewController:vc animated:YES];
}


- (UICollectionView *)peopleCollectionView{
    if (!_peopleCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setItemSize:CGSizeMake(real_size(100), real_size(140))];
        [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        layout.minimumInteritemSpacing = 0.0;
        layout.minimumLineSpacing = 0.0;
        _peopleCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _peopleCollectionView.delegate = self;
        _peopleCollectionView.dataSource = self;
        _peopleCollectionView.backgroundColor=[UIColor whiteColor];
        [_peopleCollectionView registerClass:[NewsMasterCell class] forCellWithReuseIdentifier:@"NewsMasterCell"];
        
        [self addSubview:_peopleCollectionView];
        [_peopleCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.top.equalTo(titleBgView.mas_bottom);
            make.height.mas_equalTo(@(real_size(140)));
        }];
    }
    return _peopleCollectionView;
}


@end
